package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.davemorrissey.labs.subscaleview.ImageSource;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityOriBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class OriActivity extends BaseAc<ActivityOriBinding> {

    public static boolean type = false;//ture竖 false横
    public static List<String> listPath = new ArrayList<>();
    protected boolean isPicTooLarge = false;//判断图片是否过大
    private Bitmap OriBitmap;//横竖图的bitmap
    private Dialog myTipDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_ori;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        mDataBinding.ivOriBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivOriSave.setOnClickListener(this);
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivOriSave://保存
                mDataBinding.ivOriSave.setEnabled(false);
                saveImg();
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        if (type){
            mDataBinding.tvOriTitle.setText(getString(R.string.title_shu_split));
        }else {
            mDataBinding.tvOriTitle.setText(getString(R.string.title_heng_split));
        }
        //设置图片
        doPinTu(type);
    }

    //保存拼图
    private void saveImg() {
        showDialog(getString(R.string.save_pic_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<Boolean>() {

                    @Override
                    public void doBackground(ObservableEmitter<Boolean> emitter) {
                        if (OriBitmap != null) {
                            ImageUtils.save2Album(OriBitmap, Bitmap.CompressFormat.PNG);
                        }
                        emitter.onNext(true);
                    }

                    @Override
                    public void accept(Boolean isSave) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_album);
                        mDataBinding.ivOriSave.setEnabled(true);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //设置图片横竖
    protected void doPinTu(final boolean vertical) {
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                isPicTooLarge = false;
                Bitmap bitmap1 = null;
                try {
                    bitmap1 = BitmapFactory.decodeFile(listPath.get(0));
                    bitmap1 = ImageUtils.rotate(bitmap1, ImageUtils.getRotateDegree(listPath.get(0)),
                            bitmap1.getWidth() / 2, bitmap1.getHeight() / 2, true);
                    for (int i = 1; i < listPath.size(); i++) {
                        Bitmap bitmap2 = BitmapFactory.decodeFile(listPath.get(i));
                        bitmap2 = ImageUtils.rotate(bitmap2, ImageUtils.getRotateDegree(listPath.get(i)),
                                bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, true);
                        if (!vertical) {
                            bitmap1 = horizontalPintu(bitmap1, bitmap2);
                        } else {
                            bitmap1 = pintu(bitmap1, bitmap2);
                        }
                    }
                } catch (OutOfMemoryError e) {
                    //e.printStackTrace();
                    isPicTooLarge = true;
                }
                emitter.onNext(bitmap1);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (isPicTooLarge) {
                    ToastUtils.showShort(R.string.pic_is_too_large);
                }
                if (bitmap != null) {
                    mDataBinding.mySubImgView.setImage(ImageSource.bitmap(bitmap));
                }
                //获取要保存的图片bitmap
                OriBitmap = bitmap;
            }
        });
    }

    //竖向
    private Bitmap pintu(Bitmap bitmap1, Bitmap bitmap2) {
        Bitmap ret;
        int width = bitmap1.getWidth();
        if (width > 1024) {
            int h1 = bitmap1.getHeight() * 1024 / bitmap1.getWidth();
            bitmap1 = ImageUtils.scale(bitmap1, 1024, h1, true);
            width = 1024;
        }
        if (bitmap2.getWidth() != width) {
            int h2 = bitmap2.getHeight() * width / bitmap2.getWidth();
            ret = Bitmap.createBitmap(width, bitmap1.getHeight() + h2, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            Bitmap newSizeBitmap2 = ImageUtils.scale(bitmap2, width, h2, true);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(newSizeBitmap2, 0, bitmap1.getHeight(), null);
        } else {
            ret = Bitmap.createBitmap(width, bitmap1.getHeight() + bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(bitmap2, 0, bitmap1.getHeight(), null);
        }
        return ret;
    }

    //横向
    private Bitmap horizontalPintu(Bitmap bitmap1, Bitmap bitmap2) {
        Bitmap ret;
        int height = bitmap1.getHeight();
        if (height > 1024) {
            int w1 = bitmap1.getWidth() * 1024 / bitmap1.getHeight();
            bitmap1 = ImageUtils.scale(bitmap1, w1, 1024, true);
            height = 1024;
        }
        if (bitmap2.getHeight() != height) {
            int w2 = bitmap2.getWidth() * height / bitmap2.getHeight();
            ret = Bitmap.createBitmap(bitmap1.getWidth() + w2, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            Bitmap newSizeBitmap2 = ImageUtils.scale(bitmap2, w2, height, true);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(newSizeBitmap2, bitmap1.getWidth(), 0, null);
        } else {
            ret = Bitmap.createBitmap(height, bitmap1.getWidth() + bitmap2.getWidth(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(bitmap2, bitmap1.getWidth(), 0, null);
        }
        return ret;
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y=50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}